<template>
  <div class="xd-LearningHome-Learing">
    <el-container class="xd-el-container">
      <el-aside width="16vw" class="xd-el-aside">
        <UserCenterInfo></UserCenterInfo>
        <el-card class="box-card m_t_b_1vw" shadow="hover">
          <div slot="header">
            <div class="tag">学习资源</div>
          </div>
          <el-tree :data="treeData" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </el-card>
      </el-aside>
      <el-main class="xd-el-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>
<script>
const UserCenterInfo = () => import('@/components/UserCenterInfo')

function hasOwn(value, key) {
  return Object.prototype.hasOwnProperty.call(value, key)
}
// eslint-disable-next-line
var serializeTree = (function f(data) {
  const resultList = []
  for (var idx = 0, len = data.length; idx < len; idx++) {
    const element = data[idx]
    if (hasOwn(element, 'parentId')) {
      const result = {
        id: element.id,
        label: element.name,
        children: [],
        type: 1,
        root: element
      }
      if (element.children.length) {
        result.children = result.children.concat(f(element.children))
      }
      if (element.curriculumList.length) {
        result.children = result.children.concat(f(element.curriculumList))
      }
      resultList.push(result)
    } else {
      resultList.push({
        id: element.curriculumId,
        label: element.curriculumTitle,
        type: 2,
        root: element
      })
    }
  }
  return resultList
})

const HOME_PATH = '/LearningHome'
const DETAIL_PATH = '/LearningHome/Learning/CourseIntroduction'

export default {
  name: 'Learing',
  components: {
    UserCenterInfo
  },
  data() {
    return {
      //   treeData: [{
      //     label: '所有课件',
      //     id: 0,
      //     children: [{
      //       label: '二级 1-1',
      //       children: [{
      //         label: '三级 1-1-1'
      //       }]
      //     }]
      //   }, {
      //     label: '党的理论',
      //     children: [{
      //       label: '二级 1-1',
      //       children: [{
      //         label: '三级 1-1-1'
      //       }]
      //     }]
      //   }],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  created() {
    this.getTypeList()
    // this.getUserInfo()
    this.$store.commit('updateActiveMenu', '/LearningHome')
  },
  methods: {
    handleNodeClick(opt) {
      if (opt.type === 1) {
        this.$store.commit('updateActiveCurriculumType', opt.id)
        if (this.$route.path !== HOME_PATH) {
          this.$router.push({
            path: HOME_PATH
          })
        }
      } else {
        if (this.$route.path !== DETAIL_PATH) {
          this.$router.push({
            path: '/LearningHome/Learning/CourseIntroduction',
            query: {
              tid: opt.root.curriculumId
            }
          })
        } else {
          this.$store.commit('updateactiveCurriculumId', opt.root.curriculumId)
        }
      }
    },
    getTypeList() {
      utils.post('/curriculum/my/resourceList', {}, res => {
        if (res.success) {
          // 左侧导航栏
          const result = res.data[0]
          const treeData = serializeTree(result.children)
          let rootData = {
            id: result.id,
            label: result.name,
            type: 1,
            children: []
          }
          rootData.children = result.curriculumList.map(item => {
            return {
              id: item.curriculumId,
              label: item.curriculumTitle,
              children: [],
              type: 2,
              root: item
            }
          })
          treeData.unshift(rootData)

          // 默认展示第一个分类
          this.$store.commit('updateActiveCurriculumType', res.data[0].id)
          this.$store.commit('updateCurriculumTypeList', treeData)
          this.treeData = treeData
        }
      })
    },
    openCourseIntroduction(item) {
      this.$router.push({
        path: '/LearningHome/Learning/CourseIntroduction',
        query: {
          tid: item.curriculumId
        }
      })
    }
    // getUserInfo() {
    //   utils.post(utils.api.examInfo.userCenter, {}, res => {
    //     if (res.success) {
    //       var userInfo = {
    //         name: res.data.userName,
    //         credits: res.data.points,
    //         unitRanking: res.data.deptOrder,
    //         collegeRanking: res.data.schoolOrder,
    //         cumulativeHours: Math.floor(res.data.studyTimeLong / 60),
    //         examsCount: res.data.examinationNum,
    //         waitWorkCount: res.data.manageOfficeNum,
    //         todoCount: res.data.scheduleNum
    //       }
    //       this.$store.commit('setUserInfo', userInfo)
    //     }
    //   })
    // }
  }
}
</script>
<style lang="less">
// 防止对应的 css 文件内容为空
.xd-LearningHome-Learing {
  font-size: inherit;
}
</style>